/**
 * oss 服务接口
 * @author niko
 */
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
let aliOSS = require('ali-oss');
let co = require('co');
const path = require("path");
const common_util_1 = require("./common-util");
const const_common_1 = require("../const-common");
let ossClientInstance;
/**
 * oss服务对象，只提供put Object操作
 * @date   2015-09-11
 * @author niko
 * @param  {String}   accessKeyId     阿里OSS Id
 * @param  {String}   accessKeySecret 阿里OSS 密钥
 * @param  {String}   keyPath         保存在阿里OSS的路径,e.g. /qianmicom/d2c/dev/:adminID/[pc|mobile]/
 */
class Client {
    constructor(ossConfig) {
        // if (!(this instanceof Client)) {
        // 	return new Client(ossConfig);
        // }
        this.prefixName = "";
        this.requestTimeout = "";
        var DEFAULT_OPTIONS = {
            accessKeyId: ossConfig.accessKeyId,
            accessKeySecret: ossConfig.accessKeySecret,
            bucket: 'qianmi-web',
            region: 'oss-cn-hangzhou'
        };
        this.oss = aliOSS(DEFAULT_OPTIONS);
        //根据环境信息来确定的
        // Object 前缀// 这个配置文件 可以 不在环境中的配置信息中出现. 没必要..
        this.prefixName = ossConfig.path || (const_common_1.constCommon.systemCode + '/' + common_util_1.default.getEnvCode()); //环境信息
        this.requestTimeout = ossConfig.requestTimeout || ossConfig.timeout || 15000; //环境信息
    }
    /**
     * 保存文件至阿里OSS
     * @date   2015-09-11
     * @author niko
     * @param  {String|Buffer|ReadStream}   src      filepath, new Buffer(), ReadStream
     * @param  {String}   adminID  商户id
     * @param  {String}   distName 存放的文件名字
     * @param  {Boolean}  isPC     是否PC端资源
     */
    putObject(src, distName) {
        //使用path可以把"/"的问题解决了
        var name = path.join(this.prefixName, distName); //; this.prefixName+ distName;
        //log4js.info("发送文件",src,'到阿里云上,name:',name);
        return co(this.oss.put(name, src, { timeout: this.requestTimeout }));
    }
    ;
    /**
     *
     * @param src
     * @param distName
     * @returns {*}
     */
    putObjectAbsolute(src, absoluteDistName) {
        console.log("发送文件到阿里云上,name:", absoluteDistName);
        return co(this.oss.put(absoluteDistName, src, { timeout: this.requestTimeout }));
    }
    ;
    /**
     * 返回值 是一个buffer 对象..
     * @param distName
     * @returns {*}
     */
    getObject(distName) {
        var name = this.prefixName + distName;
        //log4js.info("获取文件::",name);
        return co(this.oss.get(name));
    }
    getObjectAbsolute(absoluteDistName) {
        console.log("从阿里云获取文件::", absoluteDistName);
        return co(this.oss.get(absoluteDistName));
    }
    ;
    /**
     * 删除数据
     * @param distName
     * @returns {*}
     */
    delObject(distName) {
        var name = distName;
        //log4js.info("删除文件::",name);
        return co(this.oss.delete(name));
    }
    ;
    listObject(distDir) {
        //log4js.info('获取目录:',distDir," 下的文件列表!");
        return co(this.oss.list({ prefix: this.prefixName + distDir }));
    }
    /**
     *
     * @param options :{prefix:absoluteDistDir,"max-keys":1000}
     *                或 "x-site/prod/d2cStore/A1078241/view/";
     * @return {*}
     */
    listObjectAbsolute(absoluteDistDir) {
        //log4js.info('获取目录:',absoluteDistDir," 下的文件列表!");
        let options = {};
        if (typeof (absoluteDistDir) === 'string') {
            options['prefix'] = absoluteDistDir;
        }
        else {
            options = absoluteDistDir;
        }
        return co(this.oss.list(options));
    }
    /**
     * 批量删除
     * @param distNames
     * @returns {*}
     */
    delMultiObjectAbsolute(distNames, options) {
        //log4js.info("批量删除文件::",distNames);
        return co(this.oss.deleteMulti(distNames, options));
    }
    ;
}
exports.Client = Client;
function getOssClient(ossConfig) {
    if (ossClientInstance) {
        return ossClientInstance;
    }
    else if (ossConfig && ossConfig.accessKeyId && ossConfig.accessKeySecret) {
        return ossClientInstance = new Client(ossConfig);
    }
    else {
        throw new Error('必须传入阿里云OSS配置文件');
    }
}
exports.getOssClient = getOssClient;
